\documentclass[a4paper]{article}

% PACKAGES
\usepackage{amsmath}						%math
\usepackage{amssymb}						%math symbols
\usepackage[]{fixmath}						%fix som math thingies
\usepackage[]{mathtools}						%extra math tools
\usepackage{multicol}						%span rows
\usepackage{multirow}						%span columns
\usepackage{calc}							%calculate some stuff
\usepackage{subfig}							%subfigures
\usepackage{color}							%colors
\usepackage{bold-extra}						%allow \textsl{\textbf{ ... }}
\usepackage[english]{babel}					%correct spelling
\usepackage{caption}						%adjust captions
\usepackage{xparse}							%parse code
\usepackage[]{biblatex}						%bibliography
\usepackage{url}							%better url display
\usepackage{booktabs}						%cuter tables
\usepackage{verbatim}						%display code
\usepackage{tikz}							%tikz
\usepackage{pgfplots}						%tikz-plots
\usepackage{tikz-3dplot}						%3d drawing in tikz
\usepackage{fancyhdr}						%headers en footers
\usepackage{hyperref}						%allow hyperlinks etc
\usepackage{siunitx}						%typesetting correct SI numbers and stuff...
\usepackage{MnSymbol}						%math symbols
\usepackage{parskip}						%Don't indets but leave whitespace
\usepackage{esint}							%better integral than MnSymbol
\usepackage{refcount}						%numerical shizzle with \ref
\usepackage{wrapfig}						%wrapfigures
\usepackage{minted}							%typeset code
% \usepackage[bottom]{footmisc} 				%footnotes are set to bottom of page
\usepackage{suffix}							%starred commands
\usepackage{fancyvrb}						%better verbatim
\usepackage{xargs}							%better command and environment definitions
\usepackage{xparse}							%better command and environment definitions
\usepackage{environ}						%different environment handling
\usepackage{mathrsfs}						%extra math alphabeth
\usepackage[mathcal]{euscript}				%better mathcal
\usepackage[T1]{fontenc}						%better fonts
\usepackage[babel]{microtype}					%beter text alignment
\usepackage{filecontents}					%make bibfile in preamble


% FIGURE DIMENSIONS
\newdimen\figurewidth
\figurewidth 7.5 cm
\newdimen\figureheight
\figureheight 4 cm


% PACKAGE SETUPS
%% hyperref
\hypersetup{
	colorlinks, 
	breaklinks, 
	linktocpage,
	linkcolor=black, 
	citecolor=black, 
	filecolor=black, 
	urlcolor=black,
	}

%% caption
\captionsetup{
	font={scriptsize,sl},
	labelsep=colon,
	labelfont={bf},
	width=\figurewidth,
	}

%% quicktikz
\sisetup{
	quotient-mode=fraction,
	inter-unit-separator=\cdot,
	separate-uncertainty,
	multi-part-units = brackets,
	per-mode=fraction,
	detect-display-math=true,
	}

%% fancyvrb
\RecustomVerbatimEnvironment{Verbatim}{Verbatim}{tabsize=4}

%% minted
\usemintedstyle{tango}
\renewcommand{\theFancyVerbLine}{\textcolor[rgb]{0.4,0.4,0.4}{\scriptsize{\arabic{FancyVerbLine}}}}

%% environ
\newcommandx{\NewEnvironx}[5][2,3]%
{%
	\expandafter\newcommandx\csname start#1\endcsname[#2][#3]{#4}%
	\NewEnviron{#1}{\csname start#1\expandafter\endcsname\BODY #5}%
}

% TWEAKS
%% section numbers in margin
\makeatletter
\def\@seccntformat#1{\llap{\csname the#1\endcsname\quad}}
\makeatother

%% layout
\setlength{\headsep}{15pt}
\setlength{\textheight}{22cm}

%% better penalties
\relpenalty=10000
\binoppenalty=10000

%% disable microtye for verbatim
\makeatletter
\g@addto@macro\@verbatim{\microtypesetup{activate=false}}
\makeatother

%% paragraph numbering
\setcounter{secnumdepth}{5}
\renewcommand\theparagraph{(\alph{paragraph})}

% TIKZ SETUP
%% libraries
\usetikzlibrary{arrows}						%add arrow heads
\usetikzlibrary{scopes}						%enable scoping
\usetikzlibrary{calc}						%calculate stuff in tikz
\usetikzlibrary{decorations.pathmorphing}		%decorations for lines
\usetikzlibrary{decorations.markings}			%markings for lines
\usetikzlibrary{plotmarks}					%add plot marks
\usetikzlibrary{external}					%externalize figures
\usetikzlibrary{intersections}
\usetikzlibrary{spy}

%% pgf settings
\pgfplotsset{
	compat=newest,
	error bars/error bar style={%erro bar stylen
		line width=0.2pt,
		mark size=0.35pt
	},
	major tick length=0.1cm,
	minor tick length=0.05cm,
	/pgf/number format/set thousands separator={\,},
	every tick/.style={thin,color=black},
	every tick label/.style={font={\tiny\sffamily}},
	every axis legend/.append style={nodes={right}},
	every axis legend/.append style={font=\tiny},
	plot coordinates/math parser=false,
	spoel/.style={decorate,decoration={coil,amplitude=3pt, segment length=4.4pt},outer sep=2pt},
	weerstand/.style={decorate,decoration={zigzag,segment length=5pt,amplitude=3pt},outer sep=2pt}
}

%% tikz settings
\tikzstyle{every node}=[font=\scriptsize]

%% easydraw points
\def\diam{0.75pt}%point diameter

\NewDocumentCommand\point{d() g d[] g}{			%draw points easily
	\IfNoValueTF{#2} { 
		\draw[fill] (#1) node[circle,draw,inner sep=0pt,minimum size=\diam,fill]{};
	}{
		\IfNoValueTF{#4}{
			\IfNoValueTF{#3}{
				\draw[fill] (#1) coordinate(#2) node[circle,draw,inner sep=0pt,minimum size=\diam,fill]{};}{
				\draw[fill] (#1) coordinate(#2) node[circle,draw,inner sep=0pt,minimum size=\diam,fill]{} node[#3]{$#2$};}
			}{
			\IfNoValueTF{#3}{
				\draw[fill] (#1) coordinate(#2) node[circle,draw,inner sep=0pt,minimum size=\diam,fill]{} node[below]{#4};}{
				\draw[fill] (#1) coordinate(#2) node[circle,draw,inner sep=0pt,minimum size=\diam,fill]{} node[#3]{#4};}			
			}
	}
}	

%% trimLR
\newcommand{\trimLR}[2]{	\draw(#1,0)coordinate(LeftTrim);
					\draw(#2,0)coordinate(RightTrim);}%trim away left and right side
							
\tikzset{trimmed/.style={trim left=(LeftTrim), trim right=(RightTrim)}}

	%Example: \trimLR{<left>}{<right>}

% COMMANDS
%% ch pretty font
\newcommand*\ch{\fontfamily{pzc}\selectfont}		%pretty font	{\ch text}
\newcommand{\textch}[1]{{\ch #1}}				%pretty font	\textch{<text>}

%% blind text
\newcommand{\blindtext}{%
	Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.}%generate a bunch of text

\newcommand{\Blindtext}{%
	Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

	Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?

	At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.}%generate a big bunch of text

%% math
\let\Div\div
\renewcommand{\div}[0]{\ensuremath{\operatorname{div}}}		%divergent
\newcommand{\grad}[0]{\ensuremath{\operatorname{grad}}}		%gradient
\newcommand{\rot}[0]{\ensuremath{\operatorname{rot}}}			%rotor

\newcommand{\des}[1]{\ensuremath{_{\textrm{\textnormal{#1}}}}}	%descriptive index

\newcommand{\ve}[1]{\ensuremath{\mbox{\boldmath${\rm #1}$}}}	%fat vectors 

\newcommand{\ju}{\textrm{j}}								%complex j
\newcommand{\iu}{\textrm{i}}								%complex i
\newcommand{\eu}{\textrm{e}}								%exponential e
\renewcommand{\d}{\mathrm{d}}								%infinitesimal


\newcommand{\deriv}[3][]{%								%derivatories
	\frac{\d^{#1}#2}{\d #3^{#1}}}					

\newcommand{\pderiv}[3][]{%								%partial derivatories
	\frac{\partial^{#1}#2}{\partial #3^{#1}}}

\newcommand{\D}[2]{\deriv{#1}{#2}}							%first deriv.
\newcommand{\DD}[2]{\deriv[2]{#1}{#2}}						%2nd deriv
\newcommand{\PD}[2]{\pderiv{#1}{#2}}						%1st partial deriv.
\newcommand{\PDD}[2]{\pderiv[2]{#1}{#2}}					%2nd partial deriv.


\def\slashfrac#1#2{\leavevmode%							%\slashfrac for inline
    \raise.5ex\hbox{\small $#1$}%
    \kern-.1em/\kern-.15em%
    \lower.25ex\hbox{\small $#2$}}	

%% show all alphabets in math
	\newcommand{\alfabet}{ABCDEFGHIJKLMNOPQRSTUVXYZ\ abcdefghijklmnopqrstuvwxyz\ 0123456789}

	\newcommand{\mathalphabets}{%
		\hrulefill\textbf{MATH ALPHABETS}\hrulefill
		$ $\newline
		\texttt{normal:} \\
		$\alfabet$

		\texttt{mathscr:}\\ 
		$\mathscr{\alfabet}$

		\texttt{mathtt:}\\ 
		$\mathtt{\alfabet}$

		\texttt{mathbb:}\\ 
		$\mathbb{\alfabet}$

		\texttt{mathcal:}\\
		$\mathcal{\alfabet}$

		%\texttt{mathfrak:} \\
		%$\mathfrak{\alphabet}$

		\texttt{mathbf:}\\
		$\mathbf{\alfabet}$

		\texttt{mathrm:} \\
		$\mathrm{\alfabet}$\\

		\hrulefill}

%% integral and eval
\newcommandx{\integral}[4][1=,2=]{\int\limits_{\mathclap{#1}}^{\mathclap{#2}}\! #3\d #4}
\newcommandx{\eval}[2][1=,2=]{\Big|_{#1}^{#2}}

%	Example: \integral[<a>][<b>]{<funct>}{<var>}

%% label and ref for symbols
\newcounter{sc}
\setcounter{sc}{0}

\let\textlabel\label
\newcommand{\symblabel}[2]{%
	\begingroup
		\refstepcounter{sc}\textlabel{#2}%
		\stackrel{
			\stackrel{
			{\text{\tiny $\mathclap{(\roman{sc})}$}}
  			}{\text{$\downarrow$}}
		 }{#1}
 	\endgroup
}


\newcounter{refsc}
\setcounter{refsc}{0}

\newcommand{\symbref}[1]{%
	\setcounterref{refsc}{#1}%
	($\roman{refsc}$)%
}

%% footnotes
\let\oldfootnote\footnote
\renewcommand{\footnote}[2][]{\oldfootnote{#2\label{#1}}}
%reuse footnotes:
%\newcommand{\ftlabel}[2]{%
%	\addtocounter{footnote}{1}%
%	\footnotetext[\thefootnote]{%
%		\addtocounter{footnote}{-1}%
%		\refstepcounter{footnote}\label{#1}%
%		#2%
%		}%
%	$^{\ref{#1}}$%
%}
%
%\newcommand{\ftref}[1]{%
%	$^{\ref{#1}}$%
%}

%\renewcommand{\footnote}[2][]{\ftlabel{#1}{#2}}%actually adjust the footnote command

% EXAMPLE:
%	text\footnote[<label>]{<foot text>}, text\footref{<label>}.

% MATLAB FIGURE
\pgfkeys{
	/matlab plot/.cd,
	title/.default={\null},
	x label/.default={\null},
	x range/.code args={#1:#2}{\pgfkeyssetvalue{/matlab plot/x range}{xmin=#1,xmax=#2}},
	y range/.code args={#1:#2}{\pgfkeyssetvalue{/matlab plot/y range}{ymin=#1,ymax=#2}},
	legend/.code args={#1#2}{\pgfkeyssetvalue{/matlab plot/legend}{legend entries={#1},legend 		pos=#2}},
	title/.code={%
    		\pgfkeyssetvalue{/matlab plot/title}{#1}%
    		\pgfkeyssetvalue{/matlab plot/title height}{0.45cm}
	},
    x label/.code={%
    		\pgfkeyssetvalue{/matlab plot/x label}{#1}%
	},
    caption/.code={%
    		\pgfkeyssetvalue{/matlab plot/caption}{\caption{#1}}
	},
	label/.code={
		\pgfkeyssetvalue{/matlab plot/label}{\label{#1}}
		\pgfkeyssetvalue{/matlab plot/name}{#1}
		\pgfkeys{/matlab plot/namegiven=true}
	},
	name/.code={
		\pgfkeyssetvalue{/matlab plot/name}{#1}
		\pgfkeys{/matlab plot/namegiven=true}
	},	
	name/.value required,
	label/.value required
}

\newif\ifthenameisgivenPlot\pgfkeys{/matlab plot/namegiven/.is if=thenameisgivenPlot}

% Initialise
\pgfkeyssetvalue{/matlab plot/caption}{}
\pgfkeyssetvalue{/matlab plot/label}{}
\pgfkeyssetvalue{/matlab plot/name}{}
\pgfkeyssetvalue{/matlab plot/x label}{}
\pgfkeyssetvalue{/matlab plot/y label}{}
\pgfkeyssetvalue{/matlab plot/legend}{}
\pgfkeyssetvalue{/matlab plot/x range}{}
\pgfkeyssetvalue{/matlab plot/x range}{}
\pgfkeyssetvalue{/matlab plot/y range}{}
\pgfkeyssetvalue{/matlab plot/y range}{}
\pgfkeyssetvalue{/matlab plot/name}{}
\pgfkeyssetvalue{/matlab plot/namegiven}{false}


\newcommand{\thefigpos}[0]{htp}
\newcommand{\setfigpos}[1]{
	\renewcommand{\thefigpos}[0]{#1}}
	
\NewEnvironx{tikzplot}[2][1=,2=]{
\begingroup
	\pgfkeys{/matlab plot/.cd,caption={},#1}
	
	\ifthenameisgivenPlot
		\tikzsetnextfilename{\pgfkeysvalueof{/matlab plot/name}}
		\typeout{=>    Tikz plot "\pgfkeysvalueof{/matlab plot/name}" is being processed}
		
	\else 
		\pgfkeys{/errors/value required={/matlab plot/name}{}}
	\fi
	
	\begin{figure}[\thefigpos]
		\centering
		\begin{tikzpicture}[trim left=0cm, trim right=\figurewidth]           
			\begin{axis}[%
				view={0}{90},
				scale only axis,
				axis on top,
				minor tick num=1,
				width=\figurewidth,
				height=\figureheight,
				title={\textbf{\textsc{\pgfkeysvalueof{/matlab plot/title}}}},
				xlabel={\textbf{\pgfkeysvalueof{/matlab plot/x label}}},	
				ylabel={\textbf{\pgfkeysvalueof{/matlab plot/y label}}},
				\pgfkeysvalueof{/matlab plot/x range},
				\pgfkeysvalueof{/matlab plot/y range},
				\pgfkeysvalueof{/matlab plot/legend},
				mark size=0.8pt,
				#2
				]
}{
			\end{axis}
		\end{tikzpicture}
		\vspace{-0.15cm}
		\pgfkeysvalueof{/matlab plot/caption}
		\pgfkeysvalueof{/matlab plot/label}
	\end{figure}
\endgroup%
}


%\newenvironment{tikzplot*}[1][]{
%	\tikzset{external/remake next=true}
%	\begin{tikzplot}[#1]
%}{
%	\end{tikzplot}}
%>> easier use with \begin*{<stuff>}
\WithSuffix\newcommand\begin*[1]{%
	\tikzset{external/remake next=true}%
	\begin{#1}%
}

\newcommand\matlabfig[2][]{%
	\begin{tikzplot}[#1]
		\input{#2}%
	\end{tikzplot}%
}
	
\WithSuffix\newcommand\matlabfig*[2][]{
	\begin*{tikzplot}[#1]%
		\input{#2}%
	\end{tikzplot}%
}	

% QUICK TIKZ
\pgfkeys{%define the pgf keys that will be used by the quicktikz environment
	/quick tikz/.cd,
	caption/.code={%
    		\pgfkeyssetvalue{/quick tikz/caption}{\caption{#1}}
	},
	label/.code={
		\pgfkeyssetvalue{/quick tikz/label}{\label{#1}}
		\pgfkeyssetvalue{/quick tikz/name}{#1}
		\pgfkeys{/quick tikz/namegiven=true}
	},
	name/.code={
		\pgfkeyssetvalue{/quick tikz/name}{#1}
		\pgfkeys{/quick tikz/namegiven=true}
	},
	name/.value required
	label/.value required
}

\newif\ifthenameisgivenQuick\pgfkeys{/quick tikz/namegiven/.is if=thenameisgivenQuick}

% Initialise
\pgfkeyssetvalue{/quick tikz/caption}{}
\pgfkeyssetvalue{/quick tikz/label}{}
\pgfkeyssetvalue{/quick tikz/name}{}
\pgfkeyssetvalue{/quick tikz/namegiven}{false}

\NewEnvironx{quicktikz}[2][1=,2=]%
{%
\begingroup
	\pgfkeys{/quick tikz/.cd,caption={},#1}
	\ifthenameisgivenQuick
		\tikzsetnextfilename{\pgfkeysvalueof{/quick tikz/name}}
		\typeout{=>    Quick tikz image "\pgfkeysvalueof{/quick tikz/name}" is being processed}
		
	\else 
		\pgfkeys{/errors/value required={/quick tikz/name}{}}
	\fi
		
  	\begin{figure}[\thefigpos]
  		\centering
  		\begin{tikzpicture}[#2]
}%
{%
		\end{tikzpicture}
		\vspace{-0.15cm}
		\pgfkeysvalueof{/quick tikz/caption}
		\pgfkeysvalueof{/quick tikz/label}
  	\end{figure}
\endgroup%
}

%EXAMPLE:
%	\begin{quicktikz}[caption=<caption>,label=<label>][<tikzpicture options>]
%		<tikz content>
%	\end{quicktikz}


\def\preambleloaded{PRECOMILED PREAMBLE LOADED}
